package com.uam.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.uam.core.lk.utils.ResponseResult;
import com.uam.core.lk.utils.annotation.BaseErrorCode;
import com.uam.core.entity.enums.ErrorCode;
import com.uam.core.entity.model.TaskTrackDetail;
import com.uam.core.entity.model.TopTracks;
import com.uam.core.entity.model.TrackPositionDetail;
import com.uam.core.service.TrackService;

import lombok.extern.slf4j.Slf4j;

/**
 * 轨迹管理
 *
 * @author lishf
 */
@Slf4j
@RestController
@RequestMapping("/uam/route/track")
public class TrackController {

	@Autowired
	private TrackService trackService = null;

	@GetMapping(value = "/detail", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
			MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE })
	public ResponseResult<TaskTrackDetail> detail(@RequestParam(name = "task_id", required = false) int taskId) {
		long startTime = System.currentTimeMillis();
		try {
			if (taskId < 0) {
				TrackController.log.error("获取轨迹失败：task_id不合法");
				return new ResponseResult<TaskTrackDetail>(ErrorCode.ID_IS_ILLEGAL);
			}
			TaskTrackDetail taskTrackDetail = this.trackService.getTaskTrackDetail(taskId);
			if (taskTrackDetail == null) {
				TrackController.log.error("获取轨迹失败：task_id不存在");
				return new ResponseResult<TaskTrackDetail>(ErrorCode.ID_IS_NOT_EXIST);
			}
			ResponseResult<TaskTrackDetail> response = new ResponseResult<TaskTrackDetail>(taskTrackDetail);
//			TrackController.log.info("track detail cost:{}ms", System.currentTimeMillis() - startTime);
			return response;
		} catch (Throwable e) {
			TrackController.log.error("获取轨迹异常", e);
			return new ResponseResult<TaskTrackDetail>(BaseErrorCode.SpringExceptionErrorCode);
		}
	}

	@GetMapping(value = "/position", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
			MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE })
	public ResponseResult<TrackPositionDetail> position(@RequestParam(name = "task_id", required = false) int taskId) {
		long startTime = System.currentTimeMillis();
		try {
			if (taskId < 0) {
				TrackController.log.error("定位失败：task_id不合法");
				return new ResponseResult<TrackPositionDetail>(ErrorCode.ID_IS_ILLEGAL);
			}
			TrackPositionDetail track = this.trackService.position(taskId);
			if (track == null) {
				TrackController.log.error("定位失败：task_id不存在");
				return new ResponseResult<TrackPositionDetail>(ErrorCode.ID_IS_NOT_EXIST);
			}
			ResponseResult<TrackPositionDetail> response = new ResponseResult<TrackPositionDetail>(track);
			TrackController.log.info("track position cost:{}ms", System.currentTimeMillis() - startTime);
			return response;
		} catch (Throwable e) {
			TrackController.log.error("定位异常", e);
			return new ResponseResult<TrackPositionDetail>(BaseErrorCode.SpringExceptionErrorCode);
		}
	}

	@GetMapping(value = "/top", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
			MediaType.APPLICATION_OCTET_STREAM_VALUE, MediaType.TEXT_HTML_VALUE })
	public ResponseResult<TopTracks> top(@RequestParam(name = "size", required = false) int size) {
		long startTime = System.currentTimeMillis();
		try {
			TopTracks topTracks = this.trackService.topTracks(size);
			if (TopTracks.EMPTY == topTracks) {
				TrackController.log.error("top失败：无航线");
				return new ResponseResult<TopTracks>(ErrorCode.NO_DATA);
			}
			ResponseResult<TopTracks> response = new ResponseResult<TopTracks>(topTracks);
			//TrackController.log.info("track top cost:{}ms", System.currentTimeMillis() - startTime);
			return response;
		} catch (Throwable e) {
			TrackController.log.error("top异常", e);
			return new ResponseResult<TopTracks>(BaseErrorCode.SpringExceptionErrorCode);
		}
	}

}
